package com.qst.itoffer.controller;

import com.qst.itoffer.entity.Company;
import com.qst.itoffer.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;
import java.io.IOException;
import java.util.List;
@Slf4j
@Controller
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;
    @GetMapping("/add")
    public String addForm() {
        return "/manage/companyAdd"; // 返回企业添加页面
    }
    // 保存企业信息
    @PostMapping("/add")
    public String add(Company company,
                      @RequestParam("companyPicFile") MultipartFile file,
                      RedirectAttributes redirectAttributes) {
        try {
            companyService.save(company, file);
            redirectAttributes.addFlashAttribute("message", "企业信息添加成功！");
            return "redirect:/company/list";
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message", "企业信息添加失败: " + e.getMessage());
            return "redirect:/company/add";
        }
    }
    @GetMapping("/list")
    public String list(Model model) {
        List<Company> companies = companyService.getAllCompanies(); //调用你的selectAll方法

        model.addAttribute("companies", companies);
        log.info("全体companies: " + companyService.getAllCompanies());
        return "/manage/companyList";

    }

    @GetMapping("/updateSelect")
    public String updateSelect(@RequestParam("companyId") Integer companyId, Model model) {
        model.addAttribute("company", companyService.getCompanyById(companyId));
        log.info("查询company: " + companyService.getCompanyById(companyId));
        return "/manage/companyUpdate.html";
    }
    //根据id删除
    @GetMapping("/delete")
    public String delete(@RequestParam("companyId") Integer companyId) {
        companyService.deleteCompany(companyId);
        log.info("删除company: " + companyId);
        return "redirect:/company/list";
    }

    @PostMapping("/update")
    public String update(Company company, @RequestParam("companyPicFile") MultipartFile file)
            throws IOException {
        if (!file.isEmpty()) {
            String fileName = file.getOriginalFilename();
            String filePath = System.getProperty("user.dir") + "/src/main/resources/static/images";
            File dest = new File(filePath + fileName);
            file.transferTo(dest);
            company.setCompanyPic(fileName);
        }
        company.setCompanyId(company.getCompanyId());
        companyService.updateCompany(company);
        log.info("更新company: " + company);
        return "redirect:/company/list";
    }
    @GetMapping("/test")
    @ResponseBody
    public String test() {
        log.info("测试端点被访问");
        return "测试成功";
    }
}


